const fs = require('fs');
const SourceMap = require('source-map');
var path = require('path');
const open = require('open');
var proxy = require('http-proxy-middleware');
var mime = require('mime');
var cookieParase = require('cookie-parser');

var express = require('express');
var bodyParser = require('body-parser');
var Root_Path = path.resolve(__dirname);

const { readFileSync } = fs;
const { SourceMapConsumer } = SourceMap;
express.static.mime.define({
	'application/x-font-woff': ['woff'],
	'application/font-woff': ['woff']
 }); 
var app=express();
app.use(cookieParase());
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));

/*express允许跨域*/

app.all('*', function(req, res, next) {
    res.cookie('wt', 'BVzJRQFVoVmwEN1YwAT5cPFNkATI.',{maxAge: 86400000 * 10});
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    if(req.method=="OPTIONS") res.send(200);
    
    else  next();
});

app.use(express.static(path.join(__dirname, 'src')));
// app.use(serveStatic("src"));
app.get('/home',function(req,res){
     res.send('首页');
})

app.post('/getsouce',function(req,res){
  
    let line =parseInt(req.body.line)
    let column =parseInt(req.body.column)
    let rawSourceMap =JSON.parse(req.body.file)
    SourceMapConsumer.with(rawSourceMap, null, consumer => {
    const pos = consumer.originalPositionFor({
        line: line,
        column: column
    });
    res.json(pos);
    });
    
})

app.use('/', proxy({
    target: "https://blue-m.weizhipin.com/",
    //target: "http://api.kanzhun-inc.com/mock/66/",
    changeOrigin: true
}));

// app.listen(8787,'192.168.1.29');

// open('http://192.168.1.29:8787');

app.listen(3000,'172.16.25.78');

open('http://172.16.25.78:3000');